<?php

class System_UMS_SQLiteAdapter implements System_UMS_IUMSAdapter 
{

	//private $config;
	private $dbPath;
	private $db;

//*************************************************************************************************
/** set configuration for the adapted, e.g. data base connection or CSV file name */
	public function __construct($config=null)
	{
		if ($config != null)
			$this->setConfig($config);

			$this->db = new SQLite3($this->dbPath);

			//check if the table 'users' already exists, create if it doesn't
			$query_result = @$this->db->query('SELECT * FROM users');
			
			if ($query_result === false)
	        {
	        	echo "DB: table 'users' does not exist. Creating...\n";
	        	$q = 'CREATE TABLE users (name TEXT PRIMARY KEY, password TEXT,'
	        		.' age INTEGER, email TEXT)';
				$this->db->exec($q);
	        }
	}
	
//*************************************************************************************************
/** set configuration for the adapted, e.g. data base connection or CSV file name */
	public function setConfig($config)
	{
		$this->dbPath = $config;		
	}
	
//*************************************************************************************************
	public function userAdd($user)
	{
		$q="INSERT INTO users (name, password, age, email) 
			VALUES (
					'".$user->getName()."', 
					'".$user->getPassword()."', 
					".$user->getAge().", 
					'".$user->getEmail()."'
					);";
		
		
		@$this->db->exec($q);
		return $this->db->lastErrorCode();
		
		//echo "last error: " .  $this->db->lastErrorCode() . "\n";
	}

	
//*************************************************************************************************
	public function userDel($name)
	{

		$q="DELETE FROM users WHERE name = '".$name."';";
		$res = @$this->db->exec($q);
		return $this->db->lastErrorCode();
		
		//echo "last error: " .  @$this->db->lastErrorCode () . ", res=$res\n";
	}
	
//*************************************************************************************************	
	public function userMod($name)
	{
	}
	
//*************************************************************************************************
	public function userGet($name)
	{
		
		$result = $this->db->query("SELECT * FROM users WHERE name='".$name."'");
		
		//-----------------------------------------------------------------------		
        $users = array(); 
        $i = 0; 
		$res = $result->fetchArray(SQLITE3_ASSOC);
		
		//check if a specified user exists
		if( !isset($res['name']) )
		{
			return null;
		} 

		$userArray = array();
		
		$userArray['name'] = (string)$res['name'];
		$userArray['password'] = (string)$res['password'];
		$userArray['age'] = (string)$res['age'];
		$userArray['email'] = (string)$res['email'];
		
		return $userArray;
			
	}
	
//*************************************************************************************************
	public function userGetAll()
	{
		//print "userGetAll() called!\n";
		
		$result = $this->db->query('SELECT * FROM users');
		
		//-----------------------------------------------------------------------		
        $users = array(); 
        $i = 0; 
		while($res = $result->fetchArray(SQLITE3_ASSOC)){
				if(!isset($res['name'])) continue;
				
				$user = new System_UMS_User(
											(string)$res['name'], 
											(string)$res['password'], 
											(string)$res['age'], 
											(string)$res['email']
											);				
				$users[$i]=$user;
				$i++; 
		}		
		return $users;		
	}
}